package cn.stylefeng.guns.modular.manager.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.manager.entity.ArchiveMaterials;
import cn.stylefeng.guns.modular.manager.entity.ArchiveMaterialsAttach;
import cn.stylefeng.guns.modular.manager.mapper.ArchiveMaterialsAttachMapper;
import cn.stylefeng.guns.modular.manager.model.params.ArchiveMaterialsAttachParam;
import cn.stylefeng.guns.modular.manager.model.result.ArchiveMaterialsAttachResult;
import cn.stylefeng.guns.modular.manager.service.ArchiveMaterialsAttachService;
import cn.stylefeng.guns.modular.manager.service.ArchiveMaterialsService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 档案资料附件表 服务实现类
 * </p>
 *
 * @author blesslp
 * @since 2019-11-20
 */
@Service
public class ArchiveMaterialsAttachServiceImpl extends ServiceImpl<ArchiveMaterialsAttachMapper, ArchiveMaterialsAttach> implements ArchiveMaterialsAttachService {

    @Autowired private ArchiveMaterialsService archiveMaterialsService;

    @Override
    public void add(ArchiveMaterialsAttachParam param) {
        if (ToolUtil.isEmpty(param.getDazlid())) {
            throw new RuntimeException("档案附件添加时未找到所属资料");
        }
        ArchiveMaterials archiveMaterials = this.archiveMaterialsService.getById(param.getDazlid());
        ArchiveMaterialsAttach entity = getEntity(param);
        entity.setDaid(archiveMaterials.getDaid());
        entity.setScsj(new Date());
        this.save(entity);
    }

    @Override
    public void delete(ArchiveMaterialsAttachParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(ArchiveMaterialsAttachParam param) {
        ArchiveMaterialsAttach oldEntity = getOldEntity(param);
        ArchiveMaterialsAttach newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ArchiveMaterialsAttachResult findBySpec(ArchiveMaterialsAttachParam param) {
        return null;
    }

    @Override
    public List<ArchiveMaterialsAttachResult> findListBySpec(ArchiveMaterialsAttachParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ArchiveMaterialsAttachParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ArchiveMaterialsAttachParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ArchiveMaterialsAttach getOldEntity(ArchiveMaterialsAttachParam param) {
        return this.getById(getKey(param));
    }

    private ArchiveMaterialsAttach getEntity(ArchiveMaterialsAttachParam param) {
        ArchiveMaterialsAttach entity = new ArchiveMaterialsAttach();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
